﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2013_ece_csharp_snake
{
    public partial class EndForm : Form
    {
        public int Score { get; set; }
        public EndForm(int sc)
        {
            InitializeComponent();

            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            StartPosition = FormStartPosition.CenterScreen;
            Visible = true;

            loadPastScores();

            Score = sc;
            this.labelEndLoose.Text = "You're dead but you scored: " + Score;
        }

        // Creating a shadow on the border of the form
        protected override CreateParams CreateParams
        {
            get
            {

                CreateParams parameters = base.CreateParams;

                parameters.ClassStyle = (parameters.ClassStyle | 0x00020000);

                return parameters;

            }
        }

        // Move the window when click on blanck spaces
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }

        // Loading past scores from file
        public void loadPastScores()
        {
            string scoresString = "";
            List<int> list = new List<int>();
            try
            {
                // Creating a StreamRader to read the file 
                StreamReader monStreamReader = new StreamReader(@"LogScore.snak");
                string ligne = monStreamReader.ReadLine();

                // Reading 
                int temp;
                while (ligne != null)
                {
                    temp = 0;
                    ligne = monStreamReader.ReadLine();                   
                    int.TryParse(ligne, out temp);
                    if(temp != 0 ) list.Add(temp);
                }
                // Fermeture du StreamReader (attention très important) 
                monStreamReader.Close();
                for (int i = list.Count - 5; i < list.Count; i++)
                {
                    for (int j = 0; j < list[i]; j++)
                    {
                        scoresString += "|";
                    }
                    scoresString = scoresString + " (" + list[i] + ")\n";
                }
                this.labelLastScores.Text = scoresString;
            }
            catch (Exception ex)
            {
               // If there is an error
                Console.WriteLine("Une erreur est survenue au cours de la lecture !");
            }
        }

        // Close the form when ESC is pressed and continue when ENTER is pressed
        private void EndForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                labelEndContinue_Click(sender, e);
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
        }

        // Reducing the form
        private void labelReduce_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        // Closing the form
        private void labelClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void labelEndContinue_Click(object sender, EventArgs e)
        {
            //Application.Run(new LoginForm());

            //LoginForm l = new LoginForm();
            //l.ShowDialog();

            //this.Close();

            Thread t = new Thread(startLoginForm);
            t.Start();

            Close();
        }

        public static void startLoginForm()
        {
            Application.Run(new LoginForm());
        }
    }
}
